package com.java.dormitorymanager.impl;

import com.java.dormitorymanager.dao.IDormDao;
import com.java.dormitorymanager.pojo.Dorm;
import com.java.dormitorymanager.pojo.Student;
import com.java.dormitorymanager.util.BasicDao;

import java.util.List;

/**
 * @program: DormitoryManager
 * @description: IDormDao的实现类
 * @author: ShuangChenYue
 * @create: 2021-05-17 14:43
 **/
public class DormDaoImpl implements IDormDao
{

    @Override
    public List<Dorm> findAllManDorm()
    {
        return BasicDao.getObjectToList(Dorm.class,"select * from dorm where type='man'");
    }

    @Override
    public List<Dorm> findAllWomanDorm()
    {
        return BasicDao.getObjectToList(Dorm.class,"select * from dorm where type='woman'");
    }

    @Override
    public void addDorm(Dorm dorm)
    {
        BasicDao.updateData("insert into dorm(dornum,type) value(?,?)",dorm.getDornum(),dorm.getType());
    }

    @Override
    public void addMaleStudentToDorm(String dormNumber,int id)
    {
        BasicDao.updateData("update student set dornum=? where id=?",dormNumber,id);
    }

    @Override
    public void addAmount(int amount,String dormNumber)
    {
        BasicDao.updateData("update dorm set amount=? where dornum=?",amount,dormNumber);
    }

    @Override
    public List<Student> findAllMSByDormNumber(String dormNumber)
    {
        return BasicDao.getObjectToList(Student.class,"select * from student where dornum=?",dormNumber);
    }

    @Override
    public List<Student> findAllStudentsByDornum(String dornum)
    {
        return BasicDao.getObjectToList(Student.class,"select * from student where dornum=?",dornum);
    }

    @Override
    public void deleteStudentFromDormById(int id)
    {
        BasicDao.updateData("update student set dornum='null' where id=?",id);
    }

    @Override
    public void updateScoreByDornum(String dornum, int score)
    {
        BasicDao.updateData("update dorm set score=? where dornum=?",score,dornum);
    }

    @Override
    public void deleteDormByDornum(String dornum)
    {
        BasicDao.updateData("delete from dorm where dornum=?",dornum);//首先删除该宿舍
        BasicDao.updateData("update student set dornum='null' where dornum=?",dornum);//再将学生表中所有该宿舍的学生宿舍更新成null
    }

    @Override
    public void updateDormInfoById(int id, String dornum)
    {
        BasicDao.updateData("update dorm set dornum=? where id=?",dornum,id);
    }

    @Override
    public void updateStudentDornumByMetaDornum(String dornum, String metaDornum)
    {
        BasicDao.updateData("update student set dornum=? where dornum=?",dornum,metaDornum);
    }

    @Override
    public List<Dorm> showAllManDormUpWithScore()
    {
        return BasicDao.getObjectToList(Dorm.class,"select * from dorm where type='man' order by score");
    }

    @Override
    public List<Dorm> showAllManDormDownWithScore()
    {
        return BasicDao.getObjectToList(Dorm.class,"select * from dorm where type='man' order by score desc");
    }

    @Override
    public void addFemaleStudentToDorm(String dormNumber, int id)
    {
        BasicDao.updateData("update student set dornum=? where id=?",dormNumber,id);
    }

    @Override
    public List<Dorm> showAllWomanDormUpWithScore()
    {
        return BasicDao.getObjectToList(Dorm.class,"select * from dorm where type='woman' order by score");
    }

    @Override
    public List<Dorm> showAllWomanDormDownWithScore()
    {
        return BasicDao.getObjectToList(Dorm.class,"select * from dorm where type='woman' order by score desc");
    }

    @Override
    public Dorm findDormByDornum(String dornum)
    {
        return BasicDao.getObject(Dorm.class,"select * from dorm where dornum=?",dornum);
    }
}
